﻿using System;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace Masterduner.SourceStudio.Practices.DevelopmentLibrary.Utilities.DataConfiguration.Windows
{
    public partial class ConnectionStringBuilderWindow : Form
    {
        private string[] _databaseProviders;

        public ConnectionStringBuilderWindow()
        {
            InitializeComponent();
            this.InitializeConnectionStringBuilderList();
            this.InitializeExceptionHandlingMethods();
        }

        private void ConnectionStringBuilderWindow_Load(object sender, EventArgs e)
        {
            this.BackColor = ColorTranslator.FromHtml("#028760");
            this.InitializeDatabaseProviderList();
        }

        #region InitializeConnectionStringBuilderList
        private void InitializeConnectionStringBuilderList()
        {
            try
            {
                Assembly assembly = typeof(DataSet).Assembly;
                Type[] types = assembly.GetTypes();
                foreach (Type item in types)
                {
                    if (item.InheritFrom<DbConnectionStringBuilder>())
                    {
                        this.comboBox2.Items.Add(item.AssemblyQualifiedName);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region InitializeExceptionHandlingMethods
        private void InitializeExceptionHandlingMethods()
        {
            this.comboBox1.Items.Add(Data.DatabaseExceptionProcessMode.Default);
            this.comboBox1.Items.Add(Data.DatabaseExceptionProcessMode.CatchAndThrow);
            this.comboBox1.Items.Add(Data.DatabaseExceptionProcessMode.OnlyCatch);
        }
        #endregion

        #region comboBox2_SelectedIndexChanged
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                object builder = Creator.CreateInstance(Type.GetType(this.comboBox2.Text));
                this.propertyGrid1.SelectedObject = builder;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region ExecuteTimeout
        public int ExecuteTimeout
        {
            get { return (int)this.numericUpDown1.Value; }
        }
        #endregion

        #region ExceptionHandlingMode
        public int ExceptionHandlingMode
        {
            get
            {
                if (this.comboBox1.Text.IsEmptyOrWriteSpaces()) return (int)Data.DatabaseExceptionProcessMode.Default;
                else return (int)this.comboBox1.Text.ToEnum<Data.DatabaseExceptionProcessMode>();
            }
        }
        #endregion

        #region DatabaseProviders
        public string[] DatabaseProviders
        {
            get { return _databaseProviders; }
            set { _databaseProviders = value; }
        }
        #endregion

        #region InitializeDatabaseProviderList
        private void InitializeDatabaseProviderList()
        {
            foreach (string item in this.DatabaseProviders)
                this.comboBox3.Items.Add(item);
        }
        #endregion

        #region ProviderName
        public string ProviderName
        {
            get { return this.comboBox3.Text; }
        }
        #endregion

        #region ConnectionString
        public string ConnectionString
        {
            get { return this.propertyGrid1.SelectedObject.ForceTo<DbConnectionStringBuilder>().ConnectionString; }
        }
        #endregion

        #region ConnectionName
        public string ConnectionName
        {
            get { return this.textBox1.Text; }
        }
        #endregion
    }
}
